<template>
  <div>
    <van-search
      v-model="keyword"
      shape="round"
      placeholder="请输入搜索关键词"
      @search="searchSong"
    />
    <!-- 搜索下容器 -->
    <div class="search_wrap" v-if="!songList.length">
      <!-- 标题 -->
      <p class="hot_title">热门搜索</p>
      <!-- 热搜关键词容器 -->
      <div class="hot_name_wrap">
        <!-- 每个搜索关键词 -->
        <span
          v-for="item in hotList"
          :key="item.first"
          class="hot_item"
          @click="search(item.first)"
        >
          {{ item.first }}
        </span>
      </div>
    </div>
    <div class="search_wrap" v-else>
      <van-cell-group>
        <SongItem
          v-for="item in songList"
          :key="item.id"
          :title="item.name"
          :label="item.ar[0].name + '-' + item.al.name"
          :id="item.id"
        ></SongItem>
      </van-cell-group>
    </div>
  </div>
</template>

<script>
import { getHotSearchList, searchSong } from '@/api/Search'
export default {
  name: 'Search',
  data() {
    return {
      keyword: '',
      hotList: [],
      songList: [],
    }
  },
  created() {
    this.getHotSearchList()
  },
  methods: {
    async getHotSearchList() {
      const { code, result } = await getHotSearchList()
      if (code === 200) this.hotList = result.hots
    },
    async searchSong() {
      const { code, result } = await searchSong(this.keyword)
      if (code === 200) this.songList = result.songs
    },
    search(keyword) {
      this.keyword = keyword
      this.searchSong()
    },
  },
}
</script>

<style lang="less" scoped>
/* 搜索容器的样式 */
.search_wrap {
  padding: 0.266667rem;
}

/*热门搜索文字标题样式 */
.hot_title {
  font-size: 0.32rem;
  color: #666;
  margin-bottom: 5px;
}

/* 热搜词_容器 */
.hot_name_wrap {
  margin: 0.266667rem 0;
}

/* 热搜词_样式 */
.hot_item {
  display: inline-block;
  height: 0.853333rem;
  margin-right: 0.213333rem;
  margin-bottom: 0.213333rem;
  padding: 0 0.373333rem;
  font-size: 0.373333rem;
  line-height: 0.853333rem;
  color: #fff;
  background-color: #54b0f8;
  border-radius: 0.853333rem;
  border: 1px solid #54b0f8;
}
</style>
